﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Security.Cryptography;
using System.Collections.Specialized;
using System.IO;
using System.Data.SqlClient;
using System.Data.OleDb;
using System.Net;

public partial class Alipay_Return2 : System.Web.UI.Page
{
    #region 获取远程服务器ATN结果
    public String Get_Http(String a_strUrl, int timeout)
    {
        string strResult;
        try
        {
            HttpWebRequest myReq = (HttpWebRequest)HttpWebRequest.Create(a_strUrl);
            myReq.Timeout = timeout;
            HttpWebResponse HttpWResp = (HttpWebResponse)myReq.GetResponse();
            Stream myStream = HttpWResp.GetResponseStream();
            StreamReader sr = new StreamReader(myStream, Encoding.Default);
            StringBuilder strBuilder = new StringBuilder();
            while (-1 != sr.Peek())
            {
                strBuilder.Append(sr.ReadLine());
            }

            strResult = strBuilder.ToString();
        }
        catch (Exception exp)
        {

            strResult = "错误：" + exp.Message;
        }

        return strResult;
    }
    #endregion

    protected void Page_Load(object sender, EventArgs e)
    {
        string alipayNotifyURL = "https://www.alipay.com/cooperate/gateway.do?service=notify_verify";
        //string alipayNotifyURL = "http://notify.alipay.com/trade/notify_query.do?";
        string key = ""; //partner 的对应交易安全校验码（必须填写）
        string partner = ChangeHope.WebPage.PageRequest.GetQueryString("seller_id");//partner合作伙伴id（必须填写）
        string _input_charset = "utf-8";

        YXShop.BLL.SystemInfo.TerraceManage bll = new YXShop.BLL.SystemInfo.TerraceManage();
        ChangeHope.DataBase.DataByPage db = bll.GetList("[order by] payment_taxis asc", 1, " and payment_seller=" + ChangeHope.WebPage.PageRequest.GetQueryString("seller_id") + "");
        if (db.DataReader != null)
        {
            while (db.DataReader.Read())
            {
                key = db.DataReader["payment_key"].ToString();
            }
        }
        //partner合作伙伴id（必须填写）

        alipayNotifyURL = alipayNotifyURL + "&partner=" + partner + "&notify_id=" + Request.QueryString["notify_id"];

        //获取支付宝ATN返回结果，true是正确的订单信息，false 是无效的
        string responseTxt = Get_Http(alipayNotifyURL, 120000);
        int i;
        NameValueCollection coll;
        //Load Form variables into NameValueCollection variable.
        coll = Request.QueryString;

        // Get names of all forms into a string array.
        String[] requestarr = coll.AllKeys;

        //进行排序；
        string[] Sortedstr = AliPay2.BubbleSort(requestarr);

        #region 构造待md5摘要字符串

        StringBuilder prestr = new StringBuilder();

        for (i = 0; i < Sortedstr.Length; i++)
        {
            if (Request.Form[Sortedstr[i]] != "" && Sortedstr[i] != "sign" && Sortedstr[i] != "sign_type")
            {
                if (i == Sortedstr.Length - 1)
                {
                    prestr.Append(Sortedstr[i] + "=" + Request.QueryString[Sortedstr[i]]);
                }
                else
                {
                    prestr.Append(Sortedstr[i] + "=" + Request.QueryString[Sortedstr[i]] + "&");

                }
            }
        }

        prestr.Append(key);
        #endregion
        //生成Md5摘要；
        string mysign = AliPay2.GetMD5(prestr.ToString(), _input_charset);

        string sign = Request.QueryString["sign"];

        //  Response.Write(prestr.ToString());  

        //if (mysign == sign && responseTxt == "true")   //验证支付发过来的消息，签名是否正确
        //{

            //更新自己数据库的订单语句，请自己填写一下
            string OrderNo = ChangeHope.WebPage.PageRequest.GetQueryString("subject").Replace("订单号", "").Trim();

            YXShop.Model.Order.PaymentMoney payModel = new YXShop.Model.Order.PaymentMoney();
            YXShop.BLL.Order.PaymentMoney payBll = new YXShop.BLL.Order.PaymentMoney();
            payModel.OrderId = OrderNo;
            payModel.UserName = HttpContext.Current.Session["MemberName"] != null ? HttpContext.Current.Session["MemberName"].ToString() : string.Empty;
            payModel.GatheringMoney = Convert.ToDecimal(ChangeHope.WebPage.PageRequest.GetQueryString("total_fee"));
            payModel.GatheringDate = System.DateTime.Now;
            payModel.PresentTicket = 0;
            payModel.Remark = "支付宝支付订单号为:" + OrderNo + ";金额为:" + ChangeHope.WebPage.PageRequest.GetQueryString("total_fee") + ";商户ID:" + ChangeHope.WebPage.PageRequest.GetQueryString("seller_id") + "";
            payModel.NoteDate = DateTime.Now;
            payModel.NoteName = "系统自动";

            payBll.Add(payModel);

            string strOrderNO = ChangeHope.WebPage.PageRequest.GetQueryString("out_trade_no");//订单号
            string strPrice = ChangeHope.WebPage.PageRequest.GetQueryString("price");//金额


            
            db = bll.GetList("[order by] payment_taxis asc", 1, " and payment_seller=" + ChangeHope.WebPage.PageRequest.GetQueryString("seller_id") + "");
            if (db.DataReader != null)
            {
                while (db.DataReader.Read())
                {
                    key = db.DataReader["payment_key"].ToString();
                }
            }

            Response.Write("订单号：" + strOrderNO + "<br>金额：" + strPrice);     //成功，可美化该页面
        //}
        //else
        //{
        //    // Response.Write("------------------------------------------");
        //    // Response.Write("<br>Result:responseTxt=" + responseTxt);
        //    // Response.Write("<br>Result:mysign=" + mysign);
        //    // Response.Write("<br>Result:sign=" + sign);
        //    Response.Write("fail");
        //}
    }
}
